package com.twb.core.util;

import java.math.BigDecimal;
import java.sql.Time;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Locale;

import com.twb.core.exception.TypeCastException;

public class SimpleTypeConvert {

	@SuppressWarnings("unused")
	public static Object convert(Object paramObject, String paramString1, String paramString2) throws TypeCastException {
		Locale localLocale = new Locale("zh", "CN", "");
		if (paramObject == null)
			return null;
		if (paramObject.getClass().getName().equals(paramString1))
			return paramObject;
		if (("Object".equals(paramString1)) || ("java.lang.Object".equals(paramString1)))
			return paramObject;
	    String str = null;
	    Object localObject1;
	    Object localObject6;
	    if ((paramObject instanceof String)){

	    	str = "String";
	    	localObject1 = (String)paramObject;
	    	if (("String".equals(paramString1)) || ("java.lang.String".equals(paramString1)))
	    		return paramObject;
	    	if (((String)localObject1).length() == 0)
		        return null;
	    	Object localObject2;
	    	if (("Boolean".equals(paramString1)) || ("java.lang.Boolean".equals(paramString1))){
	    		localObject2 = null;
		        if (((String)localObject1).equalsIgnoreCase("TRUE"))
		        	localObject2 = Boolean.TRUE;
		        else
		        	localObject2 = Boolean.FALSE;
		        return localObject2;
	    	}
	    	if (("Double".equals(paramString1)) || ("java.lang.Double".equals(paramString1)))
	    		try{
	    			localObject2 = getNf(localLocale).parse((String)localObject1);
	    			return new Double(((Number)localObject2).doubleValue());
		        } catch (ParseException localParseException1){
		        	throw new TypeCastException("Could not convert " + (String)localObject1 + " to " + paramString1 + ": ", localParseException1);
		        }
	    	if (("BigDecimal".equals(paramString1)) || ("java.math.BigDecimal".equals(paramString1)))
		        try{
		        	BigDecimal localBigDecimal = new BigDecimal((String)localObject1);
		        	int i = ((String)localObject1).indexOf(".");
		        	int j = ((String)localObject1).length();
		        	if (i > -1){
		        		i = j - (i + 1);
		        		return localBigDecimal.setScale(i, 5);
		        	}
		        	return localBigDecimal.setScale(0, 5);
		        } catch (Exception localException1){
		        	throw new TypeCastException("Could not convert " + (String)localObject1 + " to " + paramString1 + ": ", localException1);
		        }
	    	if (("Float".equals(paramString1)) || ("java.lang.Float".equals(paramString1)))
		        try{
		        	Number localNumber = getNf(localLocale).parse((String)localObject1);
		        	return new Float(localNumber.floatValue());
		        } catch (ParseException localParseException2){
		        	throw new TypeCastException("Could not convert " + (String)localObject1 + " to " + paramString1 + ": ", localParseException2);
		        }
	    	Object localObject4;
	    	if (("Long".equals(paramString1)) || ("java.lang.Long".equals(paramString1)))
		        try{
		        	NumberFormat localNumberFormat1 = getNf(localLocale);
		        	localNumberFormat1.setMaximumFractionDigits(0);
		        	localObject4 = localNumberFormat1.parse((String)localObject1);
		        	return Long.valueOf(((Number)localObject4).longValue());
		        } catch (ParseException localParseException3){
		        	throw new TypeCastException("Could not convert " + (String)localObject1 + " to " + paramString1 + ": ", localParseException3);
		        }
	    	if (("Integer".equals(paramString1)) || ("java.lang.Integer".equals(paramString1)))
		        try{
		        	NumberFormat localNumberFormat2 = getNf(localLocale);
		        	localNumberFormat2.setMaximumFractionDigits(0);
		        	localObject4 = localNumberFormat2.parse((String)localObject1);
		        	return Integer.valueOf(((Number)localObject4).intValue());
		        } catch (ParseException localParseException4){
		        	throw new TypeCastException("Could not convert " + (String)localObject1 + " to " + paramString1 + ": ", localParseException4);
		        }
	    	java.util.Date localDate;
	    	Object localObject5;
	    	if (("Date".equals(paramString1)) || ("java.sql.Date".equals(paramString1))){
	    		if ((paramString2 == null) || (paramString2.length() == 0))
	    			try{
	    				return java.sql.Date.valueOf((String)localObject1);
	    			} catch (Exception localException2){
	    				try{
	    					localObject4 = null;
	    					if (localLocale != null)
	    						localObject4 = DateFormat.getDateInstance(3, localLocale);
	    					else
	    						localObject4 = DateFormat.getDateInstance(3);
	    					localDate = ((DateFormat)localObject4).parse((String)localObject1);
	    					return new java.sql.Date(localDate.getTime());
	    				} catch (ParseException localParseException7){
	    					throw new TypeCastException("Could not convert " + (String)localObject1 + " to " + paramString1 + ": ", localException2);
	    				}
	    			}
	    		try{
	    			SimpleDateFormat localSimpleDateFormat1 = new SimpleDateFormat(paramString2);
	    			localObject5 = localSimpleDateFormat1.parse((String)localObject1);
	    			return new java.sql.Date(((java.util.Date)localObject5).getTime());
		        } catch (ParseException localParseException5){
		        	throw new TypeCastException("Could not convert " + (String)localObject1 + " to " + paramString1 + ": ", localParseException5);
		        }
	    	}
	    	if (("Timestamp".equals(paramString1)) || ("java.sql.Timestamp".equals(paramString1))){
	    		if (((String)localObject1).length() == 10)
	    			localObject1 = (String)localObject1 + " 00:00:00";
		        if ((paramString2 == null) || (paramString2.length() == 0))
		        	try{
		        		return Timestamp.valueOf((String)localObject1);
		        	} catch (Exception localException3){
		        		try{
		        			localObject5 = null;
		        			if (localLocale != null)
		        				localObject5 = DateFormat.getDateTimeInstance(3, 3, localLocale);
		        			else
		        				localObject5 = DateFormat.getDateTimeInstance(3, 3);
		        			localDate = ((DateFormat)localObject5).parse((String)localObject1);
		        			return new Timestamp(localDate.getTime());
		        		} catch (ParseException localParseException8){
		        			throw new TypeCastException("Could not convert " + (String)localObject1 + " to " + paramString1 + ": ", localException3);
		        		}
		        	}
		        try{
		        	SimpleDateFormat localSimpleDateFormat2 = new SimpleDateFormat(paramString2);
		        	localObject6 = localSimpleDateFormat2.parse((String)localObject1);
		        	return new Timestamp(((java.util.Date)localObject6).getTime());
		        } catch (ParseException localParseException6){
		        	throw new TypeCastException("Could not convert " + (String)localObject1 + " to " + paramString1 + ": ", localParseException6);
		        }
		      }
		      throw new TypeCastException("Conversion from " + str + " to " + paramString1 + " not currently supported");
	    }
	    if ((paramObject instanceof BigDecimal)){
	    	str = "BigDecimal";
	    	localObject1 = (BigDecimal)paramObject;
	    	if ("String".equals(paramString1))
	    		return getNf(localLocale).format(((BigDecimal)localObject1).doubleValue());
	    	if (("BigDecimal".equals(paramString1)) || ("java.math.BigDecimal".equals(paramString1)))
		        return paramObject;
	    	if ("Double".equals(paramString1))
		        return Double.valueOf(((BigDecimal)localObject1).doubleValue());
	    	if ("Float".equals(paramString1))
		        return Float.valueOf(((BigDecimal)localObject1).floatValue());
	    	if ("Long".equals(paramString1))
		        return Long.valueOf(Math.round(((BigDecimal)localObject1).doubleValue()));
	    	if ("Integer".equals(paramString1))
		        return Integer.valueOf((int)Math.round(((BigDecimal)localObject1).doubleValue()));
	    	throw new TypeCastException("Conversion from " + str + " to " + paramString1 + " not currently supported");
	    }
	    if ((paramObject instanceof Double)){
	    	str = "Double";
	    	localObject1 = (Double)paramObject;
	    	if (("String".equals(paramString1)) || ("java.lang.String".equals(paramString1)))
	    		return getNf(localLocale).format(((Double)localObject1).doubleValue());
	    	if (("Double".equals(paramString1)) || ("java.lang.Double".equals(paramString1)))
		        return paramObject;
	    	if (("Float".equals(paramString1)) || ("java.lang.Float".equals(paramString1)))
		        return new Float(((Double)localObject1).floatValue());
	    	if (("Long".equals(paramString1)) || ("java.lang.Long".equals(paramString1)))
		        return Long.valueOf(Math.round(((Double)localObject1).doubleValue()));
	    	if (("Integer".equals(paramString1)) || ("java.lang.Integer".equals(paramString1)))
		        return Integer.valueOf((int)Math.round(((Double)localObject1).doubleValue()));
	    	if (("BigDecimal".equals(paramString1)) || ("java.math.BigDecimal".equals(paramString1)))
		        return new BigDecimal(((Double)localObject1).toString());
	    	throw new TypeCastException("Conversion from " + str + " to " + paramString1 + " not currently supported");
	    }
	    if ((paramObject instanceof Float)){
	    	str = "Float";
	    	localObject1 = (Float)paramObject;
	    	if ("String".equals(paramString1))
		        return getNf(localLocale).format(((Float)localObject1).doubleValue());
	    	if (("BigDecimal".equals(paramString1)) || ("java.math.BigDecimal".equals(paramString1)))
		        return new BigDecimal(((Float)localObject1).doubleValue());
	    	if ("Double".equals(paramString1))
		        return new Double(((Float)localObject1).doubleValue());
	    	if ("Float".equals(paramString1))
		        return paramObject;
	    	if ("Long".equals(paramString1))
		        return Long.valueOf(Math.round(((Float)localObject1).doubleValue()));
	    	if ("Integer".equals(paramString1))
		        return Integer.valueOf((int)Math.round(((Float)localObject1).doubleValue()));
	    	throw new TypeCastException("Conversion from " + str + " to " + paramString1 + " not currently supported");
	    }
	    if ((paramObject instanceof Long)){
	    	str = "Long";
	    	localObject1 = (Long)paramObject;
	    	if (("String".equals(paramString1)) || ("java.lang.String".equals(paramString1)))
		        return getNf(localLocale).format(((Long)localObject1).longValue());
	    	if (("Double".equals(paramString1)) || ("java.lang.Double".equals(paramString1)))
		        return new Double(((Long)localObject1).doubleValue());
	    	if (("Float".equals(paramString1)) || ("java.lang.Float".equals(paramString1)))
		        return new Float(((Long)localObject1).floatValue());
	    	if (("BigDecimal".equals(paramString1)) || ("java.math.BigDecimal".equals(paramString1)))
		        return new BigDecimal(((Long)localObject1).toString());
	    	if (("Long".equals(paramString1)) || ("java.lang.Long".equals(paramString1)))
		        return paramObject;
	    	if (("Integer".equals(paramString1)) || ("java.lang.Integer".equals(paramString1)))
		        return Integer.valueOf(((Long)localObject1).intValue());
	    	throw new TypeCastException("Conversion from " + str + " to " + paramString1 + " not currently supported");
	    }
	    Object localObject3;
	    if ((paramObject instanceof Integer)){
	    	str = "Integer";
	    	localObject1 = (Integer)paramObject;
	    	if (("String".equals(paramString1)) || ("java.lang.String".equals(paramString1)))
		        return getNf(localLocale).format(((Integer)localObject1).longValue());
	    	if (("Double".equals(paramString1)) || ("java.lang.Double".equals(paramString1)))
		        return new Double(((Integer)localObject1).doubleValue());
	    	if (("Float".equals(paramString1)) || ("java.lang.Float".equals(paramString1)))
		        return new Float(((Integer)localObject1).floatValue());
	    	if (("BigDecimal".equals(paramString1)) || ("java.math.BigDecimal".equals(paramString1))){
	    		localObject3 = ((Integer)localObject1).toString();
		        localObject6 = new BigDecimal(((Integer)localObject1).doubleValue());
		        int k = ((String)localObject3).indexOf(".");
		        int m = ((String)localObject3).length();
		        if (k > -1){
		        	k = m - (k + 1);
		        	return ((BigDecimal)localObject6).setScale(k, 5);
		        }
		        return ((BigDecimal)localObject6).setScale(0, 5);
	    	}
	    	if (("Long".equals(paramString1)) || ("java.lang.Long".equals(paramString1)))
		        return Long.valueOf(((Integer)localObject1).longValue());
	    	if (("Integer".equals(paramString1)) || ("java.lang.Integer".equals(paramString1)))
		        return paramObject;
	    	throw new TypeCastException("Conversion from " + str + " to " + paramString1 + " not currently supported");
	    }
	    if ((paramObject instanceof java.sql.Date)){
		      str = "Date";
		      localObject1 = (java.sql.Date)paramObject;
		      if (("String".equals(paramString1)) || ("java.lang.String".equals(paramString1))){
		    	  if ((paramString2 == null) || (paramString2.length() == 0))
		    		  return ((java.sql.Date)localObject1).toString();
		    	  localObject3 = new SimpleDateFormat(paramString2);
		    	  return ((SimpleDateFormat)localObject3).format(new java.util.Date(((java.sql.Date)localObject1).getTime()));
		      }
		      if (("Date".equals(paramString1)) || ("java.sql.Date".equals(paramString1)))
		    	  return paramObject;
		      if (("Time".equals(paramString1)) || ("java.sql.Time".equals(paramString1)))
		    	  throw new TypeCastException("Conversion from " + str + " to " + paramString1 + " not currently supported");
		      if (("Timestamp".equals(paramString1)) || ("java.sql.Timestamp".equals(paramString1)))
		    	  return new Timestamp(((java.sql.Date)localObject1).getTime());
		      throw new TypeCastException("Conversion from " + str + " to " + paramString1 + " not currently supported");
	    }
	    if ((paramObject instanceof Timestamp)){
	    	str = "Timestamp";
	    	localObject1 = (Timestamp)paramObject;
	    	if (("String".equals(paramString1)) || ("java.lang.String".equals(paramString1))){
	    		if ((paramString2 == null) || (paramString2.length() == 0))
	    			return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((java.util.Date)localObject1).toString();
		        localObject3 = new SimpleDateFormat(paramString2);
		        return ((SimpleDateFormat)localObject3).format(new java.util.Date(((Timestamp)localObject1).getTime()));
	    	}
	    	if (("Date".equals(paramString1)) || ("java.sql.Date".equals(paramString1)))
		        return new java.sql.Date(((Timestamp)localObject1).getTime());
	    	if (("Time".equals(paramString1)) || ("java.sql.Time".equals(paramString1)))
		        return new Time(((Timestamp)localObject1).getTime());
	    	if (("Timestamp".equals(paramString1)) || ("java.sql.Timestamp".equals(paramString1)))
		        return paramObject;
	    	throw new TypeCastException("Conversion from " + str + " to " + paramString1 + " not currently supported");
	    }
	    if ((paramObject instanceof Boolean)){
	    	str = "Boolean";
	    	localObject1 = (Boolean)paramObject;
	    	if (("Boolean".equals(paramString1)) || ("java.lang.Boolean".equals(paramString1)))
		        return localObject1;
	    	if (("String".equals(paramString1)) || ("java.lang.String".equals(paramString1)))
		        return ((Boolean)localObject1).toString();
	    	if (("Integer".equals(paramString1)) || ("java.lang.Integer".equals(paramString1))){
	    		if (((Boolean)localObject1).booleanValue())
	    			return Integer.valueOf(1);
	    		return Integer.valueOf(0);
	    	}
	    	throw new TypeCastException("Conversion from " + str + " to " + paramString1 + " not currently supported");
	    }
	    if (("String".equals(paramString1)) || ("java.lang.String".equals(paramString1)))
	    	return paramObject.toString();
	    throw new TypeCastException("Conversion from " + paramObject.getClass().getName() + " to " + paramString1 + " not currently supported");
	}

	private static NumberFormat getNf(Locale paramLocale){
		NumberFormat localNumberFormat = null;
		if (paramLocale == null)
			localNumberFormat = NumberFormat.getNumberInstance();
		else
			localNumberFormat = NumberFormat.getNumberInstance(paramLocale);
		    localNumberFormat.setGroupingUsed(false);
		    return localNumberFormat;
	}

	public static Boolean convert2SBoolean(Object paramObject) throws TypeCastException{
		return (Boolean)convert(paramObject, "Boolean", null);
	}

	public static Integer convert2Integer(Object paramObject) throws TypeCastException{
		return (Integer)convert(paramObject, "Integer", null);
	}

	public static String convert2String(Object paramObject) throws TypeCastException{
		Object localObject = convert(paramObject, "String", null);
		if (localObject != null)
			return localObject.toString();
		return "";
	}

	public static String convert2String(Object paramObject, String paramString) throws TypeCastException{
		Object localObject;
		if ((paramObject instanceof BigDecimal))
			localObject = convert(paramObject, "BigDecimal", null);
		else
			localObject = convert(paramObject, "String", null);
		if (localObject != null)
			return localObject.toString();
		return paramString;
	}

	public static Long convert2Long(Object paramObject) throws TypeCastException{
		return (Long)convert(paramObject, "Long", null);
	}

	public static Double convert2Double(Object paramObject) throws TypeCastException{
		return (Double)convert(paramObject, "Double", null);
	}

	public static BigDecimal convert2BigDecimal(Object paramObject, int paramInt) throws TypeCastException{
		return ((BigDecimal)convert(paramObject, "BigDecimal", null)).setScale(paramInt, 5);
	}

	public static java.sql.Date convert2SqlDate(Object paramObject, String paramString) throws TypeCastException{
		return (java.sql.Date)convert(paramObject, "Date", paramString);
	}

	public static Timestamp convert2Timestamp(Object paramObject, String paramString) throws TypeCastException{
		return (Timestamp)convert(paramObject, "Timestamp", paramString);
	}
}
